package cn.edu.nwafu.hjpg.controller;

import cn.edu.nwafu.hjpg.common.HttpResult;
import cn.edu.nwafu.hjpg.entity.Hyxxb;
import cn.edu.nwafu.hjpg.entity.Lhsxb;
import cn.edu.nwafu.hjpg.entity.Stwhb;
import cn.edu.nwafu.hjpg.service.StwhbService;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/stwhb")
public class StwhbController {
    final StwhbService stwhbService;

    public StwhbController(StwhbService stwhbService) { this.stwhbService = stwhbService;}

    @GetMapping(value = "/")
    @ResponseBody
    public HttpResult get(@RequestParam(value = "labelName",defaultValue = "",required = false) String name,
                          @RequestParam(value = "elementId",defaultValue = "",required = false) String id,
                          @RequestParam(value = "flag",defaultValue = "",required = false) String flag) {
        return HttpResult.ok(stwhbService.findByIdAndFlagInStwhb(id,name,flag));
    }
    @PostMapping(value = "/update")
    @ResponseBody
    public HttpResult update(@RequestBody Stwhb stwhb) {
        return HttpResult.ok(stwhbService.updateStwhb(stwhb));
    }
    @PostMapping(value = "/add")
    @ResponseBody
    public HttpResult add(@RequestBody(required = false) Stwhb stwhb,@RequestParam(value = "labelName",defaultValue = "",required = false) String name) {
        switch(name){
            case "环境危害分类":
                return HttpResult.ok(stwhbService.addHjwhfl(stwhb));
            case "鱼类":
                return HttpResult.ok(stwhbService.addYl(stwhb));
            case "水生无脊椎生物":
                return HttpResult.ok(stwhbService.addSswjzsw(stwhb));
            case "藻类":
                return HttpResult.ok(stwhbService.addZl(stwhb));
            case "水生植物":
                return HttpResult.ok(stwhbService.addSszw(stwhb));
            case "水生微生物":
                return HttpResult.ok(stwhbService.addSswsw(stwhb));
            case "其他水生":
                return HttpResult.ok(stwhbService.addQtsssw(stwhb));
            case "底栖生物":
                return HttpResult.ok(stwhbService.addDxsw(stwhb));
            case "陆生节肢生物":
                return HttpResult.ok(stwhbService.addLsjzsw(stwhb));
            case "陆生植物":
                return HttpResult.ok(stwhbService.addLszw(stwhb));
            case "陆生土壤生物":
                return HttpResult.ok(stwhbService.addLstrsw(stwhb));
            case "陆生土壤微生物":
                return HttpResult.ok(stwhbService.addLstrwsw(stwhb));
            case "鸟类":
                return HttpResult.ok(stwhbService.addNl(stwhb));
            case "其他陆生生物":
                return HttpResult.ok(stwhbService.addQtlssw(stwhb));
            case "其他生态毒性":
                return HttpResult.ok(stwhbService.addQtstdx(stwhb));
            default:
                return null;
        }

    }

    @GetMapping(value = "/addRecord")
    @ResponseBody
    public HttpResult querystwhbByPage(@RequestParam(value = "flag", defaultValue = "") String flag,
                                       @RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                       @RequestParam(value = "pageSize", defaultValue = "20") int pageSize){

        Pageable pageable = PageRequest.of(pageNumber - 1, pageSize);
        return HttpResult.ok(stwhbService.findAddRecordStwhb(flag,pageable));
    }

    @GetMapping(value = "/find")
    @ResponseBody
    public HttpResult findstwhbByPage(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                      @RequestParam(value = "pageSize", defaultValue = "7") int pageSize,
                                      @RequestParam(value = "attribute", defaultValue = "") String attribute,
                                      @RequestParam(value = "flag", defaultValue = "1") int flag,
                                      @RequestParam(value = "id",defaultValue = "",required = false) String id){

        Pageable pageable = PageRequest.of(pageNumber - 1, pageSize);
        return HttpResult.ok(stwhbService.findByPageStwhb(id,attribute,flag,pageable));
    }

    @PostMapping(value = "/addupdateFlag")
    @ResponseBody
    public HttpResult addupdateFlag(@RequestBody Stwhb stwhb) {
        System.out.println(stwhb.toString());
        return HttpResult.ok(stwhbService.addupdateFlag(stwhb));
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public HttpResult deladdRecordhyxxb(Stwhb stwhb){
        stwhbService.deladdRecordstwhb(stwhb);
        return HttpResult.ok();
    }
    @PostMapping(value = "/import")
    @ResponseBody
    public HttpResult addExcelInStwhb(@RequestBody List<Stwhb> stwhbList) {
        for (int i = 0 ; i <stwhbList.size() ; i ++ ){
            stwhbList.get(i).setFlag("1");

            stwhbService.addElement(stwhbList.get(i));
        }
        return HttpResult.ok();
    }
    @GetMapping(value = "/all")
    @ResponseBody
    public HttpResult findAll(@RequestParam(value = "elementId",defaultValue = "",required = false) String id,
                              @RequestParam(value = "flag",defaultValue = "",required = false) String flag) {
        System.out.println("**********************");
        return HttpResult.ok(stwhbService.findAllByBaseinfoWzbmAndFlag(id,flag));
    }
    @GetMapping(value = "/export")
    @ResponseBody
    public HttpResult get() {
        return HttpResult.ok(stwhbService.findAll());
    }

}
